package com.xps.purifierapp.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xps.purifierapp.R;
import com.xps.purifierapp.bean.OrderBean;
import com.xps.purifierapp.date.net.UserNetWorks;
import com.xps.purifierapp.view.wigdet.CustomDatePicker;
import com.xps.purifierapp.view.wigdet.LoadingDialog;
import com.zhy.autolayout.AutoLinearLayout;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import rx.Subscriber;

/**
 * Created by liwenhan on 2017/11/6.
 */


public class StatisticsFragment extends Fragment implements View.OnClickListener {
    private CustomDatePicker customDatePicker;
    ImageView actionbarIvBack;
    TextView actionbarTvTitle;
    public TextView TextViewDate;
    SimpleDateFormat sdf;
    AutoLinearLayout lly_num;
    AutoLinearLayout lly_wcddzs;
    AutoLinearLayout Linear_Distribution;
    AutoLinearLayout Linear_Complete;
    TextView TextView_Complete;
    TextView TextView_Distribution;
    ImageView ImageView_Distribution;
    ImageView ImageView_Complete;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_statistics, null);
        actionbarIvBack = view.findViewById(R.id.actionbar_iv_back);
        actionbarTvTitle = view.findViewById(R.id.actionbar_tv_title);
        TextViewDate = view.findViewById(R.id.TextView_Date);
        lly_num = view.findViewById(R.id.lly_fpnum);
        lly_wcddzs = view.findViewById(R.id.lly_wcddzs);
        Linear_Distribution = view.findViewById(R.id.Linear_Distribution);
        Linear_Complete = view.findViewById(R.id.Linear_Complete);
        TextView_Complete = view.findViewById(R.id.TextView_Complete);
        TextView_Distribution = view.findViewById(R.id.TextView_Distribution);
        ImageView_Distribution = view.findViewById(R.id.ImageView_Distribution);
        ImageView_Complete = view.findViewById(R.id.ImageView_Complete);
        lly_num.setOnClickListener(this);
        lly_wcddzs.setOnClickListener(this);
        TextViewDate.setOnClickListener(this);
        actionbarIvBack.setVisibility(View.GONE);
        actionbarTvTitle.setText("业务统计");
        initDatePicker();

        //获取分配到的订单数
        return view;
    }

    public void shuaxin() {
        fpOrder(dataOne(TextViewDate.getText().toString()));
    }

    public void fpOrder(String date) {
        UserNetWorks.getfpOrder(date, new Subscriber<OrderBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(OrderBean orderBean) {
                if (orderBean.getReturn_code().equals("SUCCESS")) {
                    TextView_Distribution.setText("" + orderBean.getReturn_body().getDistribution_num());
                    TextView_Complete.setText("" + orderBean.getReturn_body().getOver_order_num());
                }
            }
        });


    }

    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commit();
    }

    private void initDatePicker() {
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());
        TextViewDate.setText(now.split(" ")[0]);
        fpOrder(dataOne(TextViewDate.getText().toString()));

        customDatePicker = new CustomDatePicker(getActivity(), new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间

                TextViewDate.setText(time.split(" ")[0]);
//          呵呵。！？
//                String year_Now=time.split("-")[0];
//                String mounth_Now=time.split("-")[1];
//                String day_Now=time.split("-")[2];
//                String times=year_Now+mounth_Now+day_Now;
                fpOrder(dataOne(TextViewDate.getText().toString()));
            }

        }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker.showSpecificTime(false); // 不显示时和分
        customDatePicker.setIsLoop(false); // 不允许循环滚动
    }


    //将时间转换成时间戳
    public String dataOne(String time) {
        sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdf.parse(time);
            long l = date.getTime();
            String stf = String.valueOf(l);
            times = stf.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.TextView_Date:
                customDatePicker.show(TextViewDate.getText().toString());
                break;
            case R.id.lly_fpnum:
                if (Linear_Distribution.getVisibility() == View.GONE) {
                    Linear_Distribution.setVisibility(View.VISIBLE);
                    ImageView_Distribution.setImageResource(R.mipmap.image_top);
                } else {
                    Linear_Distribution.setVisibility(View.GONE);
                    ImageView_Distribution.setImageResource(R.mipmap.image_button);
                }
                break;
            case R.id.lly_wcddzs:

                if (Linear_Complete.getVisibility() == View.GONE) {
                    Linear_Complete.setVisibility(View.VISIBLE);
                    ImageView_Complete.setImageResource(R.mipmap.image_top);
                } else {
                    Linear_Complete.setVisibility(View.GONE);
                    ImageView_Complete.setImageResource(R.mipmap.image_button);
                }
                break;
        }
    }
}
